package com.hskn.hss.module.ksection.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.ksection.entity.Ksection;
import com.hskn.hss.module.ksection.mapper.KsectionMapper;
import com.hskn.hss.module.ksection.service.IKsectionService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-11
 */
@Service
public class KsectionServiceImpl extends ServiceImpl<KsectionMapper, Ksection> implements IKsectionService {

    @Resource
    KsectionMapper ksectionMapper;

    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        JSONObject json = new JSONObject();
        QueryWrapper<Ksection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(Ksection::getCreateTime);
        String depId = params.getString("depId");
        if (StringUtils.isNotEmpty(depId)) {
            queryWrapper.lambda().eq(Ksection::getDepid, depId);
        }
        String type = params.getString("type");
        if (StringUtils.isNotEmpty(type)) {
            queryWrapper.lambda().eq(Ksection::getType, type);
        }
        String name = params.getString("name");
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.lambda().like(Ksection::getName, name);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<Ksection> ksectionPage = new Page<>(page, size, true);
        IPage<Ksection> iPage = ksectionMapper.selectPage(ksectionPage, queryWrapper);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("ksections", iPage.getRecords());
        return json;
    }

    @Override
    public void mUpdate(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("ksectionId"))) {
            throw new HssException("参数错误");
        }
        Ksection ksection = JSON.parseObject(params.toJSONString(), Ksection.class);
        ksectionMapper.updateById(ksection);
    }

    @Override
    public void add(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        Ksection ksection = JSON.parseObject(params.toJSONString(), Ksection.class);
        ksection.setCreateUid(uid);
        ksectionMapper.insert(ksection);
    }

    @Override
    public void del(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("ksectionIds"))) {
            throw new HssException("参数错误");
        }
        String idStr = params.getString("ksectionIds");
        String[] ids = idStr.split(",");
        ksectionMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public Ksection detail(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("ksectionId"))) {
            throw new HssException("参数错误");
        }
        Ksection ksection = ksectionMapper.selectById(params.getString("ksectionId"));
        return ksection;
    }
}
